package com.nl.controller.web.service;

import java.util.ArrayList;
import java.util.List;

import com.nl.controller.web.bean.*;
import com.nl.controller.web.mapper.SmsAreaMapper;
import com.nl.controller.xuyi.mapper.XuYiLineMapper;
import com.nl.controller.xuyi.mapper.XuYiPointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.global.GlobalEnum;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.MD5;
import com.nl.controller.web.mapper.LoginMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private CommonService commonService;

    @Autowired
    private SmsAreaMapper smsAreaMapper;

    @Autowired
    private XuYiLineMapper xuYiLineMapper;

    @Autowired
    private XuYiPointMapper xuYiPointMapper;

    @SystemServiceLog(description = "用户登录", value = false)
    public SmsUsers login(String msisdn, String password) {
        SmsUsers user;
        MD5 md5 = new MD5();
        password = md5.getMD5ofStr(password);        
        List<SmsUsers> list = new ArrayList<>();
        try {
        	log.info(msisdn+","+password);
            list = loginMapper.login(msisdn,password);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("登录异常：" + msisdn+ ","+e.toString());
            throw new SmsException("登录异常：" + msisdn);
        }

        if (list.size() == 0) {
            throw new SmsException(GlobalEnum.USER_NOT_EXIT);

        } else {
            user = list.get(0);
            String mgrId = user.getMgrId();
            if (mgrId != "" && mgrId != null) {
                String arr[] = mgrId.split(",");
                List mgrList = new ArrayList();
                for (int i = 0; i < arr.length; i++) {
                    SmsMgrTemplateInfo smsMgrInfo;
                    try {
                        smsMgrInfo = loginMapper.getMgrInfo(arr[i]);
                        mgrList.add(smsMgrInfo);
                    } catch (Exception e) {
                        e.printStackTrace();
                        log.error("获取用户项目信息异常" + arr[i]);
                        throw new SmsException("获取用户项目信息异常：" + arr[i]);
                    }
                }
                user.setList(mgrList);
            }
            if (!password.equals(user.getPassword())) {
                log.error("登录异常：" + msisdn);
                throw new SmsException(GlobalEnum.PASSWORD_ERROR);
            }
        }
        return user;
    }

    @SystemServiceLog(description = "获取菜单", value = false)
    public List<SmsFunction> getMenu(String id) {
        List<SmsFunction> list = new ArrayList<>();
        List<SmsFunction> listAll = new ArrayList<>();
        try {
            listAll = loginMapper.getMenu(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取菜单出现异常：" + id);
            throw new SmsException("获取菜单出现异常：" + id);
        }
        //先选出父节点，然后给父节点加上子节点
        for (SmsFunction function : listAll) {
            if ("1".equals(function.getNodeTag())) {
                log.info("fatherFunction:{}", function.toString());
                for (SmsFunction smsFunction : listAll) {
                    if (smsFunction.getFatherFunctionId().equals(function.getFunctionId())
                            && "0".equals(smsFunction.getNodeTag())) {
                        log.info("Function:{}", smsFunction.toString());
                        function.getList().add(smsFunction);
                    }
                }
                list.add(function);
            }
        }
        return list;
    }


    @SystemServiceLog(description = "获取权限列表", value = false)
    public List<TreeInfo> getFuncitons() {
        TreeInfo paTreeInfo = null;
        TreeInfo treeInfo = null;
        List<TreeInfo> list = new ArrayList<>();
        List<SmsFunction> listAll;
        try {
            listAll = loginMapper.getFunctions();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取权限列表异常");
            throw new SmsException("获取权限列表异常");
        }

        for (SmsFunction paFunction : listAll) {
            if ("1".equals(paFunction.getNodeTag())) {
                paTreeInfo = new TreeInfo();
                paTreeInfo.setTitle(paFunction.getFunctionName());
                paTreeInfo.setValue(paFunction.getFunctionId());
                list.add(paTreeInfo);
                for (SmsFunction smsFunction : listAll) {
                    if (smsFunction.getFatherFunctionId().equals(paFunction.getFunctionId())
                            && "0".equals(smsFunction.getNodeTag())) {
                        treeInfo = new TreeInfo();
                        treeInfo.setTitle(smsFunction.getFunctionName());
                        treeInfo.setValue(smsFunction.getFunctionId());
                        paTreeInfo.getData().add(treeInfo);
                    }
                }
            }
        }
        return list;
    }

    //@SystemServiceLog(description = "发送短信", value = false)
    public int sendCode(String msisdn,String content) {
        int result;
        try {
            result = loginMapper.sendCode(msisdn,content);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("插入短信异常：" + msisdn+content);
            throw new SmsException("插入短信异常：" + msisdn+content);
        }
        return result;
    }
/*
    public void moveLineData(){
        List<String> lineNameList;
        XuYiLineInfo xuYiLineInfo = new XuYiLineInfo();
        lineNameList = smsAreaMapper.findXuYiSubPathNames();
        int result =0;
        int count = 0;
        //
        for(String lineName : lineNameList){
            xuYiLineInfo.setName(lineName);
            xuYiLineInfo.setOperatorTel("17856941469");
            xuYiLineInfo.setState("1");
            xuYiLineInfo.setCheckState("2");
            result = xuYiLineMapper.addLineInfo(xuYiLineInfo);
            if(result==1){
                count++;
            }
        }
        System.out.println("共入路线表"+count+"条");
    }

    public void movePointData(){
        List<String> pointNameList;

        List<XuYiStation> subStationList;

        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();

        int insertResult = 0;
        int count=0;
        pointNameList = smsAreaMapper.findXuYiSubStationNames("");
        for(String pointName : pointNameList){
            subStationList = smsAreaMapper.findXuYiSubStations("",pointName,"");
            String gpsLngLats = "";
            String gpsLngLat = "";
            String bdLngLats = "";
            String bdLngLat = "";
            for(XuYiStation pointInfo : subStationList){
                gpsLngLat = pointInfo.getLng()+","+pointInfo.getLat();
                bdLngLat = pointInfo.getBdLng()+","+pointInfo.getBdLat();
                if("".equals(gpsLngLats)){
                    gpsLngLats = gpsLngLat;
                }else{
                    gpsLngLats = gpsLngLats +"|"+ gpsLngLat;
                }

                if("".equals(bdLngLats)){
                    bdLngLats = bdLngLat;
                }else{
                    bdLngLats = bdLngLats +"|"+ bdLngLat;
                }

            }
            xuYiPointInfo.setName(pointName);
            xuYiPointInfo.setGpsLngLats(gpsLngLats);
            xuYiPointInfo.setBdLngLats(bdLngLats);
            xuYiPointInfo.setCheckState("2");
            xuYiPointInfo.setState("1");
            xuYiPointInfo.setOperatorTel("17856941469");
            insertResult = xuYiPointMapper.addPointInfo(xuYiPointInfo);
            if(insertResult==1){
                count++;
            }
        }
        System.out.println("新入point表"+count+"个数据");
    }

    public void insertRel(){
        List<XuYiLineInfo> lineInfoList ;
        List<XuYiPointInfo> pointInfoList;
        List<XuYiStation> subStationList;
        XuYiPointInfo xuYiPointInfo = new XuYiPointInfo();

        String lineId;
        String lineName;
        String pointId;
        String pointName;
        XuYiLinePointRel xuYiLinePointRel = new XuYiLinePointRel();
        int count = 0;
        int insertResult = 0;


        lineInfoList = xuYiLineMapper.findLineInfo();
        for(XuYiLineInfo lineInfo : lineInfoList){
            List<String> subNames = new ArrayList<>();
            lineId = lineInfo.getId();
            lineName = lineInfo.getName();
            subStationList=smsAreaMapper.findXuYiSubStations(lineName,"","");
            for(XuYiStation subStation : subStationList){
                pointName = subStation.getName();
                if(!subNames.contains(pointName)){
                    subNames.add(pointName);
                }
            }
            for(String pointName2 : subNames){
                xuYiPointInfo.setName(pointName2);
                pointId = xuYiPointMapper.findPointInfo(xuYiPointInfo).get(0).getId();
                xuYiLinePointRel.setLineId(lineId);
                xuYiLinePointRel.setPointId(pointId);
                xuYiLinePointRel.setState("1");
                xuYiLinePointRel.setCheckState("2");
                xuYiLinePointRel.setOperatorTel("17856941469");
                insertResult =xuYiPointMapper.addLineRelPoint(xuYiLinePointRel);
                if(insertResult==1){
                    count++;
                }
            }


        }
        System.out.println("共插入路线点位关系表"+count+"条数据");
    }*/

}
